
package xmlparser;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author Captain Normality
 */
public class XMLparser {
    
    XMLReader reader;
    List<BuySellParser> parsers;
    List<String> fileNames;
    
    
    public XMLparser()
    {
        try 
        {
         reader = XMLReaderFactory.createXMLReader();
        }
        catch ( Exception e ) 
        {
            e.printStackTrace();
        }
        
        fileNames = new ArrayList<>();
        parsers = new ArrayList<>();
    }
    
    public BuySellParser readFile(String fileName)
    {
        for(int i = 0; i < fileNames.size(); i++)
        {
            if(fileNames.get(i).equals(fileName))
            {
                return parsers.get(i);
            }
        }
        
        BuySellParser temp = new BuySellParser();
        
        try
        {   
            //set the ContentHandler
            reader.setContentHandler(temp);
            
            //Parse the file
            reader.parse(new InputSource(new FileReader(fileName)));
        }
        catch ( Exception e ) 
        {
            e.printStackTrace();
        }
        
        fileNames.add(fileName);
        parsers.add(temp);
        
        return temp;
    }
}
